//==============================================================================
//3456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_
//                               (why all the numbers? I prefer 80-column text)
//
// CForcer.cpp          Member function bodies for the CForcer class.
//==============================================================================

#include "CForcer.h"    // the *ONLY* #include statement in this file.
                        // (if you need to #include another file,
                        //  put the #include statement in CForcer.h, not here!)

void CForcer::makeSpringSet(int NewSize)
//------------------------------------------------------------------------------
// ONLY FOR CForcer objects with forceType = F_SPRINGSET!!!
// Make a dyn. alloc'd a set of springs as a single force-making object.
{
int i;

    if(forceType != F_SPRINGSET)
    {
        cout << " \n!!CForcer::makeSpringSet() called on non-F_SPRINGSET object!? NOP!\n" << endl;
        return;
    }
    if(numSprings !=0 || pE0 != NULL || pE1 != NULL)
    {               // discard any existing allocated spring-endpoint memory:
        wipeSpringSet();
    }
    // OK!  Now we can allocate some NEW memory for our set of springs.
    pE0 = new int [numSprings];     // alloc. memory for all springs in our set
    pE1 = new int [numSprings];     // with 1 integer per spring,& point there.
    numSprings = NewSize;
    for(i=0; i<numSprings; i++)     // Now set benign values for all springs;
    {
        pE0[i] = 0;                 // ALL springs begin and end at particle 0.
        pE1[i] = 0;
    }
    K_restLength = 1.0;             // nominal.
    K_spring = 1.0;
}

void CForcer::wipeSpringSet(void)
//------------------------------------------------------------------------------
// FOR F_SPRINGSET ONLY!!destroy any existing dyn. alloc'd springs
{
    if(forceType != F_SPRINGSET)
    cout << "\n!!called CForcer::wipeSpringSet() on non-F_SPRINGSET object?!?!\n" << endl;
    else if(pE0 == NULL)
    cout << "\n\n!!CForcer::wipeSpringSet() -- pE0=NULL; already empty?!?!\n" << endl;
    else if(pE1 == NULL)
    cout << "\n\n!!CForcer::wipeSpringSet() -- pE1=NULL; already empty?!?!\n" << endl;
    else if(0<=numSprings && (NULL != pE0 || NULL !=pE1))
    cout << "\n\n!CForcer::wipeSpringSet() -- zero size, non-null lists!!!" << endl;
    else if(numSprings>0 && (NULL == pE0|| NULL == pE1))
    cout << "\n\n!CForcer::wipeSpringSet() -- non-zero size, NULL pointers!!!" << endl;
    else
    {
        delete pE0;         // discard memory,
        delete pE1;
        numSprings = 0;           // set size and ptrs the 'empty' values.
        pE0 = NULL;
        pE1 = NULL;
    }
}
CForcer::CForcer(void)
//------------------------------------------------------------------------------
// Default constructor
{
    forceType = F_NONE;         // default: non-existent force.
    pE0 = NULL;                 // F_SPRINGSET has no memory currently allocated
    pE1 = NULL;                 // for the endpoint pairs for a set of springs.
    numSprings = 0;
    return;
}

CForcer::~CForcer(void)
//------------------------------------------------------------------------------
// Default destructor
{
    if(numSprings !=0 && pE0 != NULL) delete pE0; // discard any memory alloc'd for
    if(numSprings !=0 && pE1 != NULL) delete pE1; // a set of springs (F_SPRINGSET)

    return;
}
